package com.lhs.Controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.lhs.DTO.blog.BlogDTO;
import com.lhs.Page.PageParam;
import com.lhs.Page.PageResult;
import com.lhs.entity.blog.article_search;
import com.lhs.entity.blog.blog_post;
import com.lhs.service.ArticleService;
import com.lhs.service.blog_post_0Service;
import com.lhs.VO.blog.BlogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.lhs.Result.Result;

import java.util.List;


@RestController
@RequestMapping("/blog")
public class BlogController {

     @Autowired
     private blog_post_0Service blogService;

     @Autowired
     private ArticleService articleService;


     /**
     * 获取随机博客列表
     * @param limit
     * @return
     */
     @GetMapping("/random")
     public Result<List<blog_post>> getRandom(@RequestParam(defaultValue = "20") int limit) {
          List<Long> ids = blogService.getRandomIds(limit);
          if (ids == null || ids.isEmpty()) {
               return Result.error("暂无数据");
          }
          List<blog_post> list = blogService.listByIds(ids);
          return Result.success(list);
     }


//新增博客
     @PostMapping("/add")
      public Result add(@RequestBody BlogDTO blog) {
         if (blog.getPublishTime()==null){
            return blogService.addBlog(blog);
         }else {
            return articleService.schedulePublish(blog);
         }

     }

     //删除博客
      @PostMapping("/delete/{id}")
      public Result delete(@PathVariable("id") Long id) {
          return blogService.removeById(id);
     }

     //修改博客
//      @PostMapping("/update/{id}")
//      public Result update(@PathVariable("id") Long id, @RequestBody BlogDTO blog) {
//       return blogService.updateById(id, blog);
//     }


//查询文章id和userid的关系
//    @GetMapping("/getUserIdAndBlogId")
//    public Result<String> getUserIdAndBlogId(@RequestParam Long userId, @RequestParam Long blogId) {
//         return blogService.getUserIdAndBlogId(userId, blogId);
//    }




     /**
     * 获取所有博客列表
     * @param pageParam
     * @return
     */
     @SaCheckRole("admin-1")
     @GetMapping("/getAllList")
     public Result<PageResult<BlogVO>> getAllList(PageParam pageParam) {
        return blogService.getAllList(pageParam);
     }


    @GetMapping("/api/all")
    private  List<article_search> getAllBlogs(){
         return blogService.getAllListblogs();
    }



}
